﻿using System;
using System.Collections.Generic;
using System.Text;
using alglib;
using Baggr.Common.Tests;

namespace Baggr
{
    public sealed class Lsa
    {
        public static double[,] Compute(double [,] source, double filterPerCent)
        {
            double[,] u;
            double[] w;
            double[,] vt;

            svd.rmatrixsvd(
                source,
                source.GetLength(0),
                source.GetLength(1),
                1,
                1,
                1,
                out w,
                out u,
                out vt);

            double filter = w[0] * filterPerCent;
            for (int i = w.Length - 1; i >= 0 && w[i] < filter; i-- )
                w[i] = 0;
            if(w[0] == 0)
                return new double[0,0];
            var res = Matrix.MulDiag(u, w);
            res = Matrix.Mul(res, vt);
            return res;
        }
    }
}
